/*
 * Copyright Eduardo Aponte 2013
 * aponteeduardo@gmail.com
 */
#include <eyelink.h>
#include "mex.h"
#include "utils.h"


INT16 _main( mxArray **ia )
{
    void FARTYPE *buf;
    INT16 code;
    code = eyelink_newest_sample( buf );
    switch ( code ) {
        mwSize t[] = {0};
        case 0:
            *ia = mxCreateCellArray(0,t); /* Return empty array */
            break;
	case -1:
	    *ia = mxCreateCellArray(0,t);
	    break;
        case SAMPLE_TYPE:
            isample2struct( *((ISAMPLE *) buf), ia );
            break;
    }
    return code;
}

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    mwSize td[2] = {1,1};
    double *status;

    /* Verify input arguments. */
    if(nrhs != 0 ) {
        mexErrMsgTxt("Incorrect number of inputs.");
    } else if( nlhs > 2 ) {
        mexErrMsgTxt("Too many output arguments.");
    }

    plhs[0] = mxCreateNumericArray( 2, td, mxDOUBLE_CLASS, mxREAL);
    status = mxGetPr(plhs[0]);

    if ( nlhs == 2 ){
        *status = (double ) _main( plhs+1 );
    } else if ( nlhs < 2 ){
        void FARTYPE *buff;
        *status = (double ) eyelink_newest_sample( buff );
    }
    return;
}
